package pikes.peak;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorSupport;

import pikes.util.Util;

public class ApplicationContextMessageEditor extends PropertyEditorSupport implements PropertyEditor {

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if (Util.hasText(text)) {
			setValue(new ApplicationContextMessageProvider(text));
		}
	}

	@Override
	public String getAsText() {
		Object value = getValue();
		if (value == null) {
			return "";
		}

		if (!(value instanceof ApplicationContextMessageProvider)) {
			return "";
		}

		ApplicationContextMessageProvider messageProvider = (ApplicationContextMessageProvider) value;

		return messageProvider.getKey();
	}
}
